﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using YJKRevitArea.ModifiedAreat;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKArchUtils.Utils;
using YJKRevitKernel.RevitTopologySuite;
using YJKGeometryFuncs.CurveUtility;

namespace YJKRevitArea.AreaSet
{
	public class Common
	{
		public static string GetUserDirectoryFile(string strDataFileName)
		{
			try
			{
				string text = Product.DataLocation + strDataFileName;
				bool exists = new FileInfo(text).Exists;
				return text;
			}
			catch (Exception)
			{
				YJKMessageBox.Show("初始化用户数据文件(" + strDataFileName + ")失败。");
			}
			return "";
		}

		public static void WriteAreaData(Element element, List<ElementId> boundaryIds, AddDataToArea labelInfo)
		{
			double num = 0.0;
			Guid guid = new Guid(Common.m_Guid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName("BoundaryElementId_XData");
				schemaBuilder.AddArrayField("Boundary", typeof(ElementId));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("Boundary");
			entity.Set<IList<ElementId>>(field, boundaryIds);
			element.SetEntity(entity);
			if (labelInfo.m_IsWithSpray != null && labelInfo.m_PeopleDensity != null)
			{
				Guid guid2 = new Guid(Common.m_SparyGuid);
				Schema schema2 = Schema.Lookup(guid2);
				if (schema2 == null)
				{
					SchemaBuilder schemaBuilder2 = new SchemaBuilder(guid2);
					schemaBuilder2.SetSchemaName("IsWithSpary_XData");
					schemaBuilder2.SetSchemaName("PeopleDensity_XData");
					schemaBuilder2.AddSimpleField("IsWithSpary", typeof(string));
					schemaBuilder2.AddSimpleField("PeopleDensity", typeof(string));
					schema2 = schemaBuilder2.Finish();
				}
				Entity entity2 = new Entity(schema2);
				Field field2 = schema2.GetField("IsWithSpary");
				entity2.Set<string>(field2, labelInfo.m_IsWithSpray);
				field2 = schema2.GetField("PeopleDensity");
				if (labelInfo.m_SelectDensityType == "人员密度(人/m^2)")
				{
					entity2.Set<string>(field2, labelInfo.m_PeopleDensity);
				}
				else
				{
					double.TryParse(labelInfo.m_PeopleDensity, out num);
					entity2.Set<string>(field2, (1.0 / num).ToString());
				}
				element.SetEntity(entity2);
			}
		}

		public static List<ElementId> ReadAreaData(Element element)
		{
			try
			{
				Schema schema = Schema.Lookup(new Guid(Common.m_Guid));
				if (schema != null)
				{
					Entity entity = element.GetEntity(schema);
					if (entity != null && entity.Schema != null)
					{
						Field field = schema.GetField("Boundary");
						return entity.Get<IList<ElementId>>(field).ToList<ElementId>();
					}
				}
			}
			catch
			{
			}
			return null;
		}

		public static List<string> ReadAreaOtherData(Element element)
		{
			List<string> list = new List<string>();
			try
			{
				Schema schema = Schema.Lookup(new Guid(Common.m_SparyGuid));
				if (schema != null)
				{
					Entity entity = element.GetEntity(schema);
					if (entity != null && entity.Schema != null)
					{
						Field field = schema.GetField("IsWithSpary");
						string item = entity.Get<string>(field);
						list.Add(item);
						Field field2 = schema.GetField("PeopleDensity");
						string item2 = entity.Get<string>(field2);
						list.Add(item2);
						return list;
					}
				}
			}
			catch
			{
			}
			return null;
		}

		public static Area CreateArea(Autodesk.Revit.DB.Document doc, List<Curve> curves)
		{
			AddDataToArea labelInfo = default(AddDataToArea);
			ViewPlan viewPlan = doc.ActiveView as ViewPlan;
			SketchPlane sketchPlane = viewPlan.SketchPlane;
			XYZ areaCurveCenterPt = new OffsetWallLine(Common.m_Revit, "CenterLine").GetAreaCurveCenterPt(curves);
			List<ElementId> list = new List<ElementId>();
			foreach (Curve curve in curves)
			{
				if (curve != null)
				{
					ModelCurve modelCurve = doc.Create.NewAreaBoundaryLine(sketchPlane, curve, viewPlan);
					list.Add(modelCurve.Id);
				}
			}
			UV uv = new UV(areaCurveCenterPt.X, areaCurveCenterPt.Y);
			Area area = doc.Create.NewArea(viewPlan, uv);
			Common.WriteAreaData(area, list, labelInfo);
			return area;
		}

		public static Area CreateArea(Autodesk.Revit.DB.Document doc, ClosedArea closedArea, FireZoneLableInfo labelInfo, LinePatternElement linePattern, ref List<ElementId> ids)
		{
			AddDataToArea labelInfo2 = default(AddDataToArea);
			labelInfo2 = labelInfo.m_AddData;
			ViewPlan viewPlan = doc.ActiveView as ViewPlan;
			SketchPlane sketchPlane = viewPlan.SketchPlane;
			OverrideGraphicSettings overrideGraphicSettings = new OverrideGraphicSettings();
			System.Drawing.Color color = Common.IntToRgbColor(labelInfo.m_lineColor);
			overrideGraphicSettings.SetProjectionLineColor(new Autodesk.Revit.DB.Color(color.R, color.G, color.B));
			overrideGraphicSettings.SetProjectionLineWeight(labelInfo.m_lineWidth);
			overrideGraphicSettings.SetProjectionLinePatternId(linePattern.Id);
			new List<Room>().AddRange(Common.m_rooms);
			closedArea = Common.GetUseCloseArea(closedArea);
			XYZ item = null;
			XYZ item2 = null;
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			Dictionary<XYZ, double> dictionary = new Dictionary<XYZ, double>();
			XYZ xyz;
			if (!Common.TryGetCloseAreaInnerPoint(closedArea, out xyz))
			{
				if (closedArea.m_innerBoundary != null && closedArea.m_innerBoundary.Count != 0)
				{
					Curve curve = closedArea.m_innerBoundary[0][0].Clone();
					curve.MakeUnbound();
					foreach (List<Curve> list3 in closedArea.m_innerBoundary)
					{
						foreach (Curve inputCurveTwo in list3)
						{
							if (Common.TryGetIntersectionPoint(curve, inputCurveTwo, out item))
							{
								list2.Add(item);
							}
						}
					}
					foreach (Curve inputCurveTwo2 in closedArea.m_outerBoundary)
					{
						if (Common.TryGetIntersectionPoint(curve, inputCurveTwo2, out item2))
						{
							list.Add(item2);
						}
					}
					if (list.Count != 0)
					{
						foreach (XYZ xyz2 in list2)
						{
							if (!dictionary.Keys.Contains(xyz2))
							{
								dictionary.Add(xyz2, list[0].DistanceTo(xyz2));
							}
						}
					}
					dictionary = (from k in dictionary
					orderby k.Value
					select k).ToDictionary((KeyValuePair<XYZ, double> k) => k.Key, (KeyValuePair<XYZ, double> k) => k.Value);
					if (dictionary.Count != 0)
					{
						xyz = Geometry.CalculatMidPoint(list[0], dictionary.First<KeyValuePair<XYZ, double>>().Key);
					}
				}
				else
				{
					xyz = Common.GetAreaCurveCenterPt(closedArea.m_outerBoundary);
				}
			}
			Transaction transaction = new Transaction(doc, "Create FireZone By Rooms");
			transaction.Start();
			ids = new List<ElementId>();
			foreach (Curve curve2 in closedArea.m_outerBoundary)
			{
				if (curve2 != null && !Geometry.LessThan(curve2.Length * 304.8, 0.8))
				{
					ModelCurve modelCurve = doc.Create.NewAreaBoundaryLine(sketchPlane, curve2, viewPlan);
					ids.Add(modelCurve.Id);
					doc.ActiveView.SetElementOverrides(modelCurve.Id, overrideGraphicSettings);
				}
			}
			foreach (List<Curve> list4 in closedArea.m_innerBoundary)
			{
				foreach (Curve curve3 in list4)
				{
					if (curve3 != null && !Geometry.LessThan(curve3.Length * 304.8, 0.8))
					{
						ModelCurve modelCurve2 = doc.Create.NewAreaBoundaryLine(sketchPlane, curve3, viewPlan);
						ids.Add(modelCurve2.Id);
						doc.ActiveView.SetElementOverrides(modelCurve2.Id, overrideGraphicSettings);
					}
				}
			}
			UV uv = new UV(xyz.X, xyz.Y);
			Area area = doc.Create.NewArea(viewPlan, uv);
			Common.WriteAreaData(area, ids, labelInfo2);
			transaction.Commit();
			return area;
		}

		private static List<Curve> SortCurves(List<Curve> curves)
		{
			List<Curve> list = new List<Curve>();
			list.Add(curves.First<Curve>());
			XYZ endPoint = curves.First<Curve>().GetEndPoint(1);
			curves.RemoveAt(0);
			int num = 0;
			while (curves.Count > 0)
			{
				num++;
				num %= curves.Count;
				if (endPoint.IsAlmostEqualTo(curves[num].GetEndPoint(0)))
				{
					list.Add(curves[num]);
					endPoint = curves[num].GetEndPoint(1);
					curves.RemoveAt(num);
					num--;
				}
				else if (endPoint.IsAlmostEqualTo(curves[num].GetEndPoint(1)))
				{
					endPoint = curves[num].GetEndPoint(0);
					list.Add(curves[num].CreateReversed());
					curves.RemoveAt(num);
					num--;
				}
			}
			curves.Clear();
			curves.AddRange(list);
			return curves;
		}

		private static ClosedArea GetUseCloseArea(ClosedArea inputCloseArea)
		{
			List<Curve> list = new List<Curve>();
			List<Curve> list2 = new List<Curve>();
			List<List<Curve>> list3 = new List<List<Curve>>();
			foreach (Curve inputCurve in inputCloseArea.m_outerBoundary)
			{
				Curve item = CurveUtility.TransformCurveAtZAixs(inputCurve, 0.0, true);
				list.Add(item);
			}
			foreach (List<Curve> list4 in inputCloseArea.m_innerBoundary)
			{
				foreach (Curve inputCurve2 in list4)
				{
					Curve item = CurveUtility.TransformCurveAtZAixs(inputCurve2, 0.0, true);
					list2.Add(item);
				}
				list3.Add(list2);
			}
			inputCloseArea.m_outerBoundary.Clear();
			inputCloseArea.m_innerBoundary.Clear();
			inputCloseArea.m_outerBoundary.AddRange(list);
			inputCloseArea.m_innerBoundary.AddRange(list3);
			return inputCloseArea;
		}

		public static Area CreateArea(Autodesk.Revit.DB.Document doc, ClosedArea closedArea, OverrideGraphicSettings settings)
		{
			AddDataToArea labelInfo = default(AddDataToArea);
			ViewPlan viewPlan = doc.ActiveView as ViewPlan;
			SketchPlane sketchPlane = viewPlan.SketchPlane;
			XYZ xyz = null;
			xyz = Common.GetAreaCurveCenterPt(closedArea.m_outerBoundary);
			List<ElementId> list = new List<ElementId>();
			foreach (Curve curve in closedArea.m_outerBoundary)
			{
				ModelCurve modelCurve = doc.Create.NewAreaBoundaryLine(sketchPlane, curve, viewPlan);
				list.Add(modelCurve.Id);
				doc.ActiveView.SetElementOverrides(modelCurve.Id, settings);
			}
			foreach (List<Curve> list2 in closedArea.m_innerBoundary)
			{
				foreach (Curve curve2 in list2)
				{
					ModelCurve modelCurve2 = doc.Create.NewAreaBoundaryLine(sketchPlane, curve2, viewPlan);
					list.Add(modelCurve2.Id);
					doc.ActiveView.SetElementOverrides(modelCurve2.Id, settings);
				}
			}
			UV uv = new UV(xyz.X, xyz.Y);
			Area area = doc.Create.NewArea(viewPlan, uv);
			Common.WriteAreaData(area, list, labelInfo);
			return area;
		}

		private static bool TryGetIntersectionPoint(Curve inputCurveOne, Curve inputCurveTwo, out XYZ interectionPoint)
		{
			interectionPoint = null;
			IntersectionResultArray intersectionResultArray = null;
			bool result;
			try
			{
                if ((int)inputCurveOne.Intersect(inputCurveTwo, out intersectionResultArray) == 8)
				{
					interectionPoint = intersectionResultArray.get_Item(0).XYZPoint;
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static XYZ GetAreaCenterPoint(List<Curve> areaCurves)
		{
			CurveArray curveArray = new CurveArray();
			foreach (Curve curve in areaCurves)
			{
				if (curve != null)
				{
					curveArray.Append(curve);
				}
			}
			XYZ xyz = new XYZ(0.0, 0.0, 0.0);
			if (curveArray.Size < 2)
			{
				return xyz;
			}
			xyz = Geometry.Centroid(curveArray);
			List<XYZ> list = new List<XYZ>();
			foreach (Curve curve2 in areaCurves)
			{
				if (curve2 != null)
				{
					list.AddRange(curve2.Tessellate());
					AssistFunc.CompressArray(list);
				}
			}
			if (1 != Geometry.PointInPloygon(list, xyz, 0.0001, 0.0001))
			{
				List<XYZ> list2 = new List<XYZ>();
				foreach (Curve curve3 in areaCurves)
				{
					if (curve3 != null)
					{
						list2.Add(curve3.GetEndPoint(0));
					}
				}
				for (int i = 0; i < list2.Count; i++)
				{
					for (int j = i + 2; j < list2.Count; j++)
					{
						XYZ xyz2 = list2[i].Add(list2[j]).Divide(2.0);
						if (1 == Geometry.PointInPloygon(list, xyz2, 0.0001, 0.0001))
						{
							xyz = xyz2;
						}
					}
				}
			}
			return xyz;
		}

		public static bool TryGetCloseAreaInnerPoint(ClosedArea input, out XYZ innerPoint)
		{
			innerPoint = null;
			bool result;
			try
			{
				if (input == null)
				{
					result = false;
				}
				else
				{
					CurveLoop curveLoop = new CurveLoop();
					List<CurveLoop> list = null;
					CurveLoop curveLoop2 = null;
					if (input.m_innerBoundary != null && input.m_innerBoundary.Count > 0)
					{
						list = new List<CurveLoop>();
					}
					foreach (Curve curve in input.m_outerBoundary)
					{
						curveLoop.Append(curve);
					}
					if (input.m_innerBoundary != null)
					{
						foreach (List<Curve> list2 in input.m_innerBoundary)
						{
							curveLoop2 = new CurveLoop();
							foreach (Curve curve2 in list2)
							{
								curveLoop2.Append(curve2);
							}
							if (list != null)
							{
								list.Add(curveLoop2);
							}
						}
					}
					result = new RevitPolygonDataBean(curveLoop, list, 3).TryGetOneInnerPoint(out innerPoint);
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static XYZ GetAreaCurveCenterPt(List<Curve> lstCurves)
		{
			List<XYZ> list = new List<XYZ>();
			XYZ xyz = new XYZ();
			foreach (Curve curve in lstCurves)
			{
				if (curve != null)
				{
					XYZ endPoint = curve.GetEndPoint(0);
					XYZ endPoint2 = curve.GetEndPoint(1);
					list.Add(new XYZ(endPoint.X, endPoint.Y, endPoint.Z));
					list.Add(new XYZ(endPoint2.X, endPoint2.Y, endPoint.Z));
				}
			}
			foreach (XYZ xyz2 in list)
			{
				xyz += xyz2;
			}
			xyz = new XYZ(xyz.X / (double)list.Count, xyz.Y / (double)list.Count, xyz.Z / (double)list.Count);
			return xyz;
		}

		public static XYZ GetAreaCenterPoint(ClosedArea closedArea)
		{
			if (closedArea.m_innerBoundary.Count == 0)
			{
				return Common.GetAreaCenterPoint(closedArea.m_outerBoundary);
			}
			foreach (Curve curve in closedArea.m_outerBoundary)
			{
				if (!(curve == null))
				{
					XYZ endPoint = curve.GetEndPoint(0);
					foreach (List<Curve> list in closedArea.m_innerBoundary)
					{
						foreach (Curve curve2 in list)
						{
							if (!(curve2 == null))
							{
								XYZ endPoint2 = curve2.GetEndPoint(0);
								XYZ xyz = endPoint.Add(endPoint2).Divide(2.0);
								if (Common.IsPointInRing(xyz, closedArea))
								{
									return xyz;
								}
							}
						}
					}
				}
			}
			return closedArea.m_innerBoundary[0][0].GetEndPoint(0) - 0.1 * XYZ.BasisX;
		}

		public static bool IsPointInRing(XYZ pt, ClosedArea closedArea)
		{
			if (Common.IsPointInArea(closedArea.m_outerBoundary, pt))
			{
				using (List<List<Curve>>.Enumerator enumerator = closedArea.m_innerBoundary.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						if (Common.IsPointInArea(enumerator.Current, pt))
						{
							return false;
						}
					}
				}
				return true;
			}
			return false;
		}

		public static GraphicsStyle CreateLineStyle(Autodesk.Revit.DB.Document doc, string lineTypeName, int lineWidth, int lineColor)
		{
			Category category = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
			if (category == null)
			{
				return null;
			}
			if (!category.SubCategories.Contains(lineTypeName))
			{
				Transaction transaction = new Transaction(doc, "NewLineStyle");
				transaction.Start();
				Category category2 = doc.Settings.Categories.NewSubcategory(category, lineTypeName);
				category2.SetLineWeight(lineWidth, (Autodesk.Revit.DB.GraphicsStyleType)1);
				System.Drawing.Color color = Common.IntToRgbColor(lineColor);
				category2.LineColor = new Autodesk.Revit.DB.Color(color.R, color.G, color.B);
				transaction.Commit();
			}
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(GraphicsStyle)))
			{
				GraphicsStyle graphicsStyle = element as GraphicsStyle;
				if (graphicsStyle != null && graphicsStyle.Name.CompareTo(lineTypeName) == 0)
				{
					return graphicsStyle;
				}
			}
			return null;
		}

		public static GraphicsStyle ModifyLineStyle(Autodesk.Revit.DB.Document doc, string lineTypeName, int lineWidth, int lineColor)
		{
			Category category = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
			if (category == null)
			{
				return null;
			}
			Transaction transaction = new Transaction(doc, "ModifyLineStyle");
			transaction.Start();
			Category category2 = category.SubCategories.get_Item(lineTypeName);
			category2.SetLineWeight(lineWidth, (Autodesk.Revit.DB.GraphicsStyleType)1);
			System.Drawing.Color color = Common.IntToRgbColor(lineColor);
			category2.LineColor = new Autodesk.Revit.DB.Color(color.R, color.G, color.B);
			transaction.Commit();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(GraphicsStyle)))
			{
				GraphicsStyle graphicsStyle = element as GraphicsStyle;
				if (graphicsStyle != null && graphicsStyle.Name.CompareTo(lineTypeName) == 0)
				{
					return graphicsStyle;
				}
			}
			return null;
		}

		public static System.Drawing.Color IntToRgbColor(int colorInt)
		{
			int red = colorInt & 255;
			int green = colorInt >> 8 & 255;
			int blue = colorInt >> 16 & 255;
			return System.Drawing.Color.FromArgb(red, green, blue);
		}

		public static int RgbColorToInt(System.Drawing.Color colorRgb)
		{
			return (int)colorRgb.B << 16 | (int)((ushort)((int)colorRgb.G << 8 | (int)colorRgb.R));
		}

		public static LinePatternElement CreateLinePatternElement(Autodesk.Revit.DB.Document doc, string patternName)
		{
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(LinePatternElement)).ToElements())
			{
				LinePatternElement linePatternElement = element as LinePatternElement;
				if (linePatternElement.Name == patternName)
				{
					return linePatternElement;
				}
			}
			List<LinePatternSegment> list = new List<LinePatternSegment>();
			list.Add(new LinePatternSegment((LinePatternSegmentType)0, 0.02));
			list.Add(new LinePatternSegment((LinePatternSegmentType)1, 0.03));
			list.Add(new LinePatternSegment((LinePatternSegmentType)0, 0.02));
			list.Add(new LinePatternSegment((LinePatternSegmentType)1, 0.03));
			LinePattern linePattern = new LinePattern(patternName);
			linePattern.SetSegments(list);
			Transaction transaction = new Transaction(doc, "Create a linepattern element");
			transaction.Start();
			LinePatternElement result = LinePatternElement.Create(doc, linePattern);
			transaction.Commit();
			return result;
		}

		public static bool IsPointInArea(List<Curve> area, XYZ pt)
		{
			List<XYZ> list = new List<XYZ>();
			foreach (Curve curve in area)
			{
				if (curve != null)
				{
					list.AddRange(curve.Tessellate());
				}
			}
			return 1 == Geometry.PointInPloygon(list, pt, 0.0001, 0.0001);
		}

		public static double mmToFeet(double val)
		{
			return val / 304.8;
		}

		public static List<Area> FilterAreas(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
			filteredElementCollector.OfClass(typeof(SpatialElement));
			List<Area> list = new List<Area>();
			foreach (Element element in filteredElementCollector)
			{
				Area area = element as Area;
				if (area != null)
				{
					list.Add(area);
				}
			}
			return list;
		}

		public static List<Area> FilterFireAreas(Autodesk.Revit.DB.Document doc, ElementId viewId)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, viewId);
			filteredElementCollector.OfClass(typeof(SpatialElement));
			List<Area> list = new List<Area>();
			foreach (Element element in filteredElementCollector)
			{
				Area area = element as Area;
				if (area != null)
				{
					ElementId elementId = area.GetParameter(BuiltInParameter.AREA_SCHEME_ID).AsElementId();
					if ((doc.GetElement(elementId) as AreaScheme).Name.Contains("防火分区"))
					{
						list.Add(area);
					}
				}
			}
			return list;
		}

		public static List<Area> FilterFireAreas(Autodesk.Revit.DB.Document doc)
		{
			ElementId id = doc.ActiveView.Id;
			return Common.FilterFireAreas(doc, id);
		}

		public static List<AreaTag> FilterAreaTags(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
			filteredElementCollector.OfClass(typeof(SpatialElementTag));
			List<AreaTag> list = new List<AreaTag>();
			foreach (Element element in filteredElementCollector)
			{
				AreaTag areaTag = element as AreaTag;
				if (areaTag != null)
				{
					list.Add(areaTag);
				}
			}
			return list;
		}

		public static List<ModelCurve> FilterModelCurves(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
			List<ModelCurve> list = new List<ModelCurve>();
			filteredElementCollector.OfClass(typeof(CurveElement)).OfCategory(BuiltInCategory.OST_AreaSchemeLines);
			foreach (Element element in filteredElementCollector)
			{
				ModelCurve modelCurve = element as ModelCurve;
				if (modelCurve != null)
				{
					list.Add(modelCurve);
				}
			}
			return list;
		}

		public static List<Room> FilterRooms(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
			List<Room> list = new List<Room>();
			filteredElementCollector.OfClass(typeof(SpatialElement)).OfCategory(BuiltInCategory.OST_Rooms);
			foreach (Element element in filteredElementCollector)
			{
				Room room = element as Room;
				if (room != null)
				{
					list.Add(room);
				}
			}
			return list;
		}

		public static void DeleteArea(Autodesk.Revit.DB.Document doc, ElementId areaId)
		{
			Area area = doc.GetElement(areaId) as Area;
			if (area == null)
			{
				return;
			}
			Transaction transaction = new Transaction(doc, "Delete Areas");
			transaction.Start();
			try
			{
				foreach (ElementId elementId in Common.ReadAreaData(area))
				{
					doc.Delete(elementId);
				}
			}
			catch
			{
				foreach (IList<BoundarySegment> list in area.GetBoundarySegments(new SpatialElementBoundaryOptions()))
				{
					foreach (BoundarySegment segment in list)
					{
						if (segment.GetElement(doc) != null)
						{
							ModelCurve modelCurve = segment.GetElement(doc) as ModelCurve;
							doc.Delete(modelCurve.Id);
						}
					}
				}
			}
			doc.Delete(areaId);
			transaction.Commit();
		}

		public static List<Line> CreateRect(XYZ pt1, XYZ pt2)
		{
			XYZ xyz = new XYZ(pt1.X, pt2.Y, pt1.Z);
			XYZ xyz2 = new XYZ(pt2.X, pt1.Y, pt1.Z);
			return new List<Line>
			{
				Line.CreateBound(pt1, xyz),
				Line.CreateBound(xyz, pt2),
				Line.CreateBound(pt2, xyz2),
				Line.CreateBound(xyz2, pt1)
			};
		}

		public static Arc CreateArc(XYZ ptCenter, XYZ ptArcStart, XYZ ptArcEnd)
		{
			double length = (ptArcStart - ptCenter).GetLength();
			XYZ xyz = ptArcStart - ptCenter;
			XYZ xyz2 = ptArcEnd - ptCenter;
			xyz.AngleOnPlaneTo(xyz2, XYZ.BasisZ);
			double num;
			double num2;
			if (xyz.AngleOnPlaneTo(xyz2, XYZ.BasisZ) <= Math.PI)
			{
				num = XYZ.BasisX.AngleOnPlaneTo(xyz, XYZ.BasisZ);
				num2 = XYZ.BasisX.AngleOnPlaneTo(xyz2, XYZ.BasisZ);
				if (num > num2)
				{
					num2 += Math.PI*2;
				}
				return Arc.Create(ptCenter, length, num, num2, XYZ.BasisX, XYZ.BasisY);
			}
			num = XYZ.BasisX.Negate().AngleOnPlaneTo(xyz, XYZ.BasisZ.Negate());
			num2 = XYZ.BasisX.Negate().AngleOnPlaneTo(xyz2, XYZ.BasisZ.Negate());
			if (num > num2)
			{
				num2 += Math.PI*2;
			}
			return Arc.Create(ptCenter, length, num, num2, XYZ.BasisX.Negate(), XYZ.BasisY);
		}

		public static List<ElementId> FilterSurfaceLayerWalls(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
			filteredElementCollector.OfClass(typeof(Wall));
			List<ElementId> list = new List<ElementId>();
			foreach (Element element in filteredElementCollector)
			{
				Wall wall = element as Wall;
				CompoundStructure compoundStructure = wall.WallType.GetCompoundStructure();
				if (compoundStructure != null)
				{
					IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
					bool flag = false;
					for (int i = 0; i < layers.Count; i++)
					{
						CompoundStructureLayer compoundStructureLayer = layers[i];
						if (1 == (int)compoundStructureLayer.Function)
						{
							flag = true;
							break;
						}
					}
					if (!flag)
					{
						list.Add(wall.Id);
					}
				}
			}
			return list;
		}

		public static bool ExistSurfaceLayerWall(Room room, Autodesk.Revit.DB.Document doc)
		{
			foreach (IList<BoundarySegment> list in room.GetBoundarySegments(new SpatialElementBoundaryOptions
			{
                SpatialElementBoundaryLocation = (SpatialElementBoundaryLocation)1
			}))
			{
				foreach (BoundarySegment segment in list)
				{
					if (segment.GetElement(doc) != null && typeof(Wall) == segment.GetElement(doc).GetType())
					{
						CompoundStructure compoundStructure = (segment.GetElement(doc) as Wall).WallType.GetCompoundStructure();
						if (compoundStructure != null)
						{
							IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
							bool flag = true;
							for (int i = 0; i < layers.Count; i++)
							{
								CompoundStructureLayer compoundStructureLayer = layers[i];
								if (1 == (int)compoundStructureLayer.Function)
								{
									flag = false;
									break;
								}
							}
							if (flag)
							{
								return true;
							}
						}
					}
				}
			}
			return false;
		}

		public static ExternalCommandData m_Revit = null;

		public static string m_WallRangeType = null;

		public static List<Room> m_rooms = new List<Room>();

		public static List<Curve> m_roomCv = new List<Curve>();

		private static string m_Guid = "A07967B8-1C19-4700-BC62-A4C55CE08717";

		private static string m_SparyGuid = "FA976B23-5B30-4483-97F9-93515B80578C";
	}
}
